package so.sao.shop.supplier.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import so.sao.shop.supplier.config.Constant;
import so.sao.shop.supplier.domain.CommMeasureSpec;
import so.sao.shop.supplier.domain.User;
import so.sao.shop.supplier.pojo.Result;
import so.sao.shop.supplier.service.CommMeasureSpecService;
import so.sao.shop.supplier.util.Ognl;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by XuPengFei on 2017/8/14.
 */
@RestController
@RequestMapping("/commMeasureSpec")
public class CommMeasureSpecController {

    @Autowired
    private CommMeasureSpecService commMeasureSpecService;

    /**
     * 查询计量规格，查询出自己的和管理员前期添加的公共的
     * @param request 请求对象
     * @param supplierId 供应商id
     * @return Result - List<CommMeasureSpec> 集合对象
     */
    @GetMapping(value = "/searchCommMeasureSpec")
    public Result search(HttpServletRequest request, Long supplierId){
        // 1. 先从请求对象中拿到供应商(用户)的ID
       User user = (User)request.getAttribute(Constant.REQUEST_USER);
        if (null == user) {   //验证用户是否登陆
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        //判断登录用户是否是供应商（供应商登陆时，supplierId从request中取，第二个参数supplierId前台传回0）
        if (null==supplierId || supplierId.equals(0L)) {
            supplierId = user.getAccountId();
        }
        // 2. 根据用户ID(supplierId),查询到自己和管理员添加的所有的计量规格
        return commMeasureSpecService.getCommMeasureSpe(supplierId);
    }

    /**
     * 新增计量规格
     * @param request 请求对象
     * @param commMeasureSpec 计量规格
     * @return Result 结果对象
     */
    @PostMapping("/save")
    public Result create(HttpServletRequest request,@RequestBody CommMeasureSpec commMeasureSpec){
       // 1. 先从请求对象中拿到供应商(用户)的ID
       User user = (User)request.getAttribute(Constant.REQUEST_USER);
        if (null == user) {   //验证用户是否登陆
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        Long supplierId = user.getAccountId();
        if(Constant.ADMIN_STATUS.equals(user.getIsAdmin())){
            supplierId = 0L;
        }
        //  效验输入的name = null ,"" 就抛出异常
        if( null == commMeasureSpec.getName() || Ognl.isEmpty(commMeasureSpec.getName().trim()) ){
            return Result.fail("计量规格不能为空！");
        }else {
            if(commMeasureSpec.getName().trim().length() > Constant.CheckMaxLength.MAX_MEASURESPEC_NAME_LENGTH){
                return Result.fail("计量规格不能超过"+ Constant.CheckMaxLength.MAX_MEASURESPEC_NAME_LENGTH+"位!");
            }
        }
        // 2. 去Service层执行新增操作
        return  commMeasureSpecService.saveCommMeasureSpec(supplierId,commMeasureSpec);
    }

    /**
     * 删除计量规格
     * @param id  计量规格ID
     *@param request 请求对象
     * @return Result 结果对象
     */
    @DeleteMapping("/delete")
    public Result delete(HttpServletRequest request,@RequestParam Long id){
       User user = (User)request.getAttribute(Constant.REQUEST_USER);
        if (null == user) {   //验证用户是否登陆
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        long supplierId = user.getAccountId();
        if(Constant.ADMIN_STATUS.equals(user.getIsAdmin())){
            supplierId = 0L;
        }
        return commMeasureSpecService.deleteById(supplierId, id);
    }

}
